package com.li.jiang.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.li.jiang.config.PhotoConfig;
import com.li.jiang.obj.pojo.CompanyPhotos;
import com.li.jiang.obj.pojo.Photos;
import com.li.jiang.obj.vo.PhotosVo;
import com.li.jiang.service.impl.CompanyPhotosServiceImpl;
import com.li.jiang.service.impl.CompanyServiceImpl;
import com.li.jiang.service.impl.PhotosServiceImpl;
import com.li.jiang.utils.ReturnMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李江
 * @since 2024-04-12
 */
@RestController
@RequestMapping("/photos")
public class PhotosController {

    @Autowired
    private PhotosServiceImpl photosService;
    @Autowired
    private CompanyPhotosServiceImpl companyPhotosService;
    @RequestMapping("/addCompanyPhotos/")
    public ReturnMessage addCompanyPhotos(Long cid,String name){
        System.out.println("上传图片："+name);
        Photos p = new Photos();
        p.setName(name);
        p.setAddress(PhotoConfig.LOCAL_FILE_ROOT_ADDRESS+name);
        p.setHref(PhotoConfig.BASE_ADDRESS+PhotoConfig.HREF+"/"+name);
        System.out.println("上传图片："+p);
        if (!photosService.save(p)){
            System.out.println("图片保存失败!!name:"+name);
            return ReturnMessage.error(name+"上传失败");
        }
        if (!companyPhotosService.addCompanyPhotos(cid, name)){
            System.out.println("图片关系保存失败!!cid:"+cid);
            return ReturnMessage.error(name+"上传失败");
        }
        System.out.println(name+"保存成功");
        return ReturnMessage.success("图片"+name+"保存成功！！");
    }
    @GetMapping("/getCompanyPhotosByCid/{cid}")
    public ReturnMessage getCompanyPhotos(@PathVariable("cid") Long cid){
        List<Photos>lists = companyPhotosService.getCompanyPhotos( cid);
        System.out.println("获取图片列表:"+ lists);
        return ReturnMessage.success("获取图片成功!",PhotosVo.of(lists));
    }
    @GetMapping("/deleteCompanyPhoto/{name}")
    public ReturnMessage delPhoto(@PathVariable("name") String name){

        if (name==null){
            return ReturnMessage.error("删除图片失败！");
        }
        if (companyPhotosService.removeByName(name)){
            return ReturnMessage.success("成功删除图片："+name);
        }
        return ReturnMessage.error("删除图片失败！");

    }
}
